//
//  DestAllHotCityViewController.m
//  TravelEveryDay
//
//  Created by dlios on 15/10/23.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "DestAllHotCityViewController.h"
#import "BaseHeader.h"
#import "DestAllHotCityModel.h"
#import "DestAllHotCityTableViewCell.h"
#import "DestFirstCellInfoViewController.h"
@interface DestAllHotCityViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray *destAllModelArray;
@property(nonatomic,strong)UITableView *destAllHotCityTableView;
@end

@implementation DestAllHotCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor  = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame =CGRectMake(0, 20, 25, 25);
    [button setImage:[UIImage imageNamed:@"play-jiantouzuo.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(DestGoBack) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    self.destAllModelArray = [[NSMutableArray alloc]init];
    _conntStart = @0;
    [self createDestAllHotCityTableView];///创建
    [self addFooterReflash];
    
}

#pragma mark - 创建tabelView;
- (void)createDestAllHotCityTableView {
    _destAllHotCityTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,  0, SCREEN_WIDTH, SCREEN_HEIGHT )];
    _destAllHotCityTableView.delegate = self;
    _destAllHotCityTableView.dataSource = self;
    _destAllHotCityTableView.backgroundColor = [UIColor colorWithRed:251/255. green:246/255. blue:233/255. alpha:1];
    [_destAllHotCityTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:_destAllHotCityTableView];
    
}
#pragma mark -上拉加载
- (void)addFooterReflash {
    __block DestAllHotCityViewController *destAll = self;
    [_destAllHotCityTableView addLegendFooterWithRefreshingBlock:^{
        
        [destAll getDestAllHotData:destAll.conntStart];
    }];
    [_destAllHotCityTableView.footer beginRefreshing];
}
#pragma mark - tabelView的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _destAllModelArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150 *HEIGHT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static   NSString * cellId =@"cell";
    DestAllHotCityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[DestAllHotCityTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
        
    }
    DestAllHotCityModel *model = [_destAllModelArray objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor colorWithRed:251/255. green:246/255. blue:233/255. alpha:1];
    [cell setSelectionStyle:0];
    cell.dataModel = model;
    return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    cell.layer.transform =CATransform3DMakeScale(0.1,0.1, 1);
    
    //设置动画时间为0.25秒,xy方向缩放的最终值为1
    
    [UIView  animateWithDuration:0.25 animations:^{
        cell.layer.transform =CATransform3DMakeScale(1,1, 1);
    }];
    
}
#pragma mark  - 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DestAllHotCityModel *model = [_destAllModelArray objectAtIndex:indexPath.row];
    DestFirstCellInfoViewController *destFirst = [[DestFirstCellInfoViewController alloc]init];
    destFirst.type = model.type;
    destFirst.MyId = model.MyId;
    [self.navigationController pushViewController:destFirst animated:YES];
    
}
#pragma mark - 请求数据
- (void)getDestAllHotData: (NSNumber *)countStart {
    
    [DestAFNDataBlock string:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/pois/all/?start=%@&count=20&sort=default&shift=false&latitude=38.88240455963685&longitude=121.53963515009309",_type,_myId,countStart] AFNData:^(id block) {
        
        NSMutableArray *itemsArray = [block objectForKey:@"items"]
        ;
        for (NSMutableDictionary *itemsDic in itemsArray) {
            DestAllHotCityModel *allCityModel = [DestAllHotCityModel modelWithDictionary:itemsDic];
            [_destAllModelArray addObject:allCityModel];
        }
        
        if ([[block objectForKey:@"next_start"] isKindOfClass:[NSNull class]]) {
            [_destAllHotCityTableView.footer endRefreshing];
            [_destAllHotCityTableView removeFooter];//移除加载
            return ;
            
        }else{
            
            _conntStart = [block objectForKey:@"next_start"];
        }
        
        [_destAllHotCityTableView.footer endRefreshing];
        [_destAllHotCityTableView reloadData];
        
    } AFNData1:^(id block) {
       
        
    }];
}
#pragma mark - 返回
- (void)DestGoBack {
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
